﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
using System.Web;

namespace DataAccess
{
   public  class AccountDA
    {
       ConnectDB cn = new ConnectDB();
       AccountEntites ace = new AccountEntites();
       CardEntites cardE = new CardEntites();
          // QUÝ
       public static DataTable GetBalance(CardEntites card)
       {
                  
           card.CardNo = HttpContext.Current.Session["CardNo"].ToString();
           DataTable dt = Data.ExeStoredWithParamToDataTable("ProAccountSelectByCardNo", "@CardNo", card.CardNo);         
           return dt;
       }

       
       // NHẤT
       public bool GetAccountID(AccountEntites a)
       {
           try { 
             SqlConnection con = new SqlConnection(cn.ConnectionString());
           con.Open();
           SqlCommand cmd = new SqlCommand("ProCheckAccountID", con);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@AccountID", a.AccountID));
           SqlDataReader dt = cmd.ExecuteReader();
           if (dt.Read())
           {
               cmd.Dispose();
               return true;
           }
           else
           {
               return false;
           }
           }catch(Exception ex)
           {
               throw ex;
           }
          
          
       }

       //QuocPN
       public bool CheckWithdraw(AccountEntites accountE, CardEntites cardE)
       {
           bool kt = false;
           try {
                SqlConnection con = new SqlConnection(cn.ConnectionString());
           con.Open();
           SqlCommand cmd = new SqlCommand("SP_CheckWithdrawByCarNo", con);
           cmd.CommandType = CommandType.StoredProcedure;         
           cmd.Parameters.Add(new SqlParameter("@Amount", accountE.Balance));
           cmd.Parameters.Add(new SqlParameter("@CardNo", cardE.CardNo));
           SqlDataReader dt = cmd.ExecuteReader();
           cmd.Dispose();
               kt=true;
           }catch(Exception ex)
           {
               throw ex;
           }
           
           return kt;
       }
       //QuocPN
       public Decimal getBalance(CardEntites cardE)
       {
           SqlConnection con = new SqlConnection(cn.ConnectionString());
           con.Open();
           SqlCommand cmd = new SqlCommand("Select_Balance", con);
           cmd.CommandType = CommandType.StoredProcedure;           
           cmd.Parameters.Add(new SqlParameter("@CardNo", cardE.CardNo));
           cmd.ExecuteNonQuery();
           DataTable dt = new DataTable();
           SqlDataAdapter da = new SqlDataAdapter();
           da.SelectCommand = cmd;
           da.Fill(dt);
           string x = dt.Rows[0][0].ToString();
           Decimal balance = Decimal.Parse(x);
           return balance;


       }
    }
}
